package com.zan.backend.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.zan.backend.enums.BaseErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 通用返回类
 */
@Data
@ApiModel("基础返回体")
@JsonIgnoreProperties(ignoreUnknown = true)
public class BaseResponse<T> {

    @ApiModelProperty("成功标识true or false")
    private Boolean success;

    /**
     * 主要是为了前端有些地方用到弹窗，有些地方进行重定向，以此来进行判断
     */
    @ApiModelProperty("错误码")
    private Integer errCode;
    @ApiModelProperty("错误消息")
    private String errMsg;
    @ApiModelProperty("返回对象")
    private T data;

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> result = new BaseResponse<T>();
        result.setData(null);
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> result = new BaseResponse<T>();
        result.setData(data);
        result.setSuccess(Boolean.TRUE);
        return result;
    }

    public static <T> BaseResponse<T> fail(Integer code, String msg) {
        BaseResponse<T> result = new BaseResponse<T>();
        result.setSuccess(Boolean.FALSE);
        result.setErrCode(code);
        result.setErrMsg(msg);
        return result;
    }

    public static <T> BaseResponse<T> fail(BaseErrorEnum errorEnum) {
        BaseResponse<T> result = new BaseResponse<T>();
        result.setSuccess(Boolean.FALSE);
        result.setErrCode(errorEnum.getErrorCode());
        result.setErrMsg(errorEnum.getErrorMsg());
        return result;
    }

    public boolean isSuccess() {
        return this.success;
    }
}

